package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.base.BaseBean
import com.hinnotek.amoeba.bean.old.AmibaInstallByIdBean
import com.hinnotek.amoeba.bean.old.SellPurchaseRequistionDetailBean
import com.hinnotek.amoeba.databinding.ActivityMsgPurchaseRequisitionDetailBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.IntentUtils
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.NoticeViewModel
import com.hinnotek.amoeba.dialog.RejectCustomDialog
import com.lee.common.network.net.ObserverState
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MsgPurchaseRequisitionDetailActivity :
    BaseActivity<ActivityMsgPurchaseRequisitionDetailBinding>(), View.OnClickListener {

    private var amibaPurchaseId = 0
    private var noticeId = 0
    private var applyUserId = 0
    private var approvalUserId: String? = null
    private var sellPurchaseRequistionDetailBean: AmibaInstallByIdBean? = null

    private val viewModel by viewModels<NoticeViewModel>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_msg_purchase_requisition_detail)
        setLeftVisible(true)
        setTitle("采购申请")
        setRightText("项目需料")?.setOnClickListener {
            if (sellPurchaseRequistionDetailBean != null) {
                val bundle = Bundle()
                bundle.putSerializable(
                    "sellPurchaseRequistionDetailBean", sellPurchaseRequistionDetailBean
                )
                IntentUtils.startActivity(
                    mContext,
                    SellProjectRequirementsDetailActivity::class.java, bundle
                )
            }
        }

    }

    override fun initVew() {
        bindingView.superButtonHistory.setOnClickListener(this)
        bindingView.superButtonReject.setOnClickListener(this)
        bindingView.superButtonAgree.setOnClickListener(this)
        val manager1 = LinearLayoutManager(this)
        manager1.orientation = RecyclerView.VERTICAL
    }

    override fun setData() {
        val type = intent.getIntExtra("type", 0)
        noticeId = intent.getIntExtra("noticeId", 0)
        if (type == 0) {
            bindingView.llMsgBtns.visibility = View.VISIBLE
        } else if (type == 1) {
            bindingView.llMsgBtns.visibility = View.GONE
        } else {
            bindingView.superButtonReject.visibility = View.GONE
            bindingView.superButtonAgree.visibility = View.GONE
        }
        getNoticeDetailById(noticeId)
    }


    override fun onClick(v: View) {
        when (v.id) {
            R.id.superButton_history -> {
                val bundle = Bundle()
                bundle.putInt("msgType", 3)
                bundle.putInt("applyUserId", applyUserId)
                bundle.putString("approvalUserId", approvalUserId)
                IntentUtils.startActivity(mContext, MsgHistoryListActivity::class.java, bundle)
            }
            R.id.superButton_reject -> {
                val rejectCustomDialog =
                    RejectCustomDialog(mContext)
                rejectCustomDialog.setOnClickBottomListener(object :
                    RejectCustomDialog.OnClickBottomListener {
                    override fun onPositiveClick() {
                        amibaPurchaseApproval(
                            rejectCustomDialog, amibaPurchaseId,
                            3, noticeId, rejectCustomDialog.editText
                        )
                    }

                    override fun onNegtiveClick() {
                        rejectCustomDialog.dismiss()
                    }
                })
                rejectCustomDialog.setCanceledOnTouchOutside(false)
                rejectCustomDialog.show()
            }
            R.id.superButton_agree -> amibaPurchaseApproval(null, amibaPurchaseId, 2, noticeId, "")
        }
    }

    /**
     * app端通过id查询单个临时缴费续费申请接口
     */
    fun getNoticeDetailById(noticeId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        viewModel.getNoticeDetailById(noticeId)
            .observe(this, object : ObserverState<AmibaInstallByIdBean>() {

                override fun onDataChange(data: AmibaInstallByIdBean) {
                    customDialog.dismiss()
                    sellPurchaseRequistionDetailBean = data
                    if (data.applyUserType == 3) { //3:研发；2：技术 1：销售
                        bindingView.llChooseUser.visibility = View.GONE
                        bindingView.llRelatedProject.visibility = View.GONE
                    } else {
                        bindingView.llChooseUser.visibility = View.VISIBLE
                        bindingView.llRelatedProject.visibility = View.VISIBLE

                        bindingView.tvChooseUser.text = data.customerName
                        bindingView.tvRelatedProject.text = data.projectName
                    }
                    applyUserId = data.applyUserId
                    approvalUserId = data.approvalUserId
                    amibaPurchaseId = data.amibaPurchaseId
                    bindingView.tvDocumentNo.text = data.documentNum
                    bindingView.tvMode.text = if (data.applicationMethod == 1) "首次" else "变更"
                    bindingView.tvDeliveryDate.text = data.deliveryDate
                    bindingView.tvProjectOverview.text = data.projectOverview
                    bindingView.tvSkillsRequirement.text = data.technicalRequirement
                    bindingView.tvApplicant.text = data.createUser
                    bindingView.tvApplicantStatus.text = "已申请"
                    bindingView.tvApplicantStatus.setTextColor(getColor21(R.color.black))
                    bindingView.tvRelatedApprover.text = data.approvalUser
                    if (data.approvalState == 1) {
                        bindingView.tvApproverStatus.text = "已申请"
                        bindingView.tvApproverStatus.setTextColor(getColor21(R.color.black))
                    } else if (data.approvalState == 2) {
                        bindingView.tvApproverStatus.text = "已同意"
                        bindingView.tvApproverStatus.setTextColor(getColor21(R.color.xui_btn_green_normal_color))
                    } else if (data.approvalState == 3) {
                        bindingView.tvApproverStatus.text = "已拒绝"
                        bindingView.tvApproverStatus.setTextColor(getColor21(R.color.xui_config_color_red))
                        bindingView.llReject.visibility = View.VISIBLE
                        bindingView.tvReject.text = data.refusalReasons
                    } else if (data.approvalState == 4) {
                        bindingView.tvApplicantStatus.text = "已撤回"
                        bindingView.tvApplicantStatus.setTextColor(getColor21(R.color.xui_config_color_red))
                    }
                    bindingView.tvSellCc.text = "宁创"
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    if (code == 400) {
                        finishActivity()
                    }
                    super.onError(msg, code)
                }

            })
    }

    /**
     * app端阿米巴采购申请审批接口
     */
    fun amibaPurchaseApproval(
        dialog: RejectCustomDialog?, amibaPurchaseId: Int, approvalState: Int,
        noticeId: Int, refusalReasons: String
    ) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        viewModel.getAmibaPurchaseApproval(amibaPurchaseId, approvalState, noticeId, refusalReasons)
            .observe(this, object : ObserverState<BaseBean>() {
                override fun onDataChange(data: BaseBean) {
                    customDialog.dismiss()
                    dialog?.dismiss()
                    finishActivity()
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }
            })
    }
}